<template>
	<Layout :class="'bg-white p-2'">
		<Row :class="'m-1'">
			<Col span="2">
			<Button class="m-2" type="error" @click="newHouse()">新增</Button>
			</Col>
			<Col span="4">
			<el-cascader :props="props" clearable placeholder="省市区选择"></el-cascader>
			</Col>
			<Col span="4">
			<Input :class="'w-45 m-2'" v-model="searchDes" placeholder="请输入描述" />
			</Col>
			<Col span="2">
			<Button class="m-2" type="primary" shape="circle" @click="search(1)" icon="ios-search">查询</Button>
			</Col>
		</Row>
		<Layout :class="'bg-white'">
			<Table border :class="'m-1'" :columns="tableInfo.columnInfo" :data="tableInfo.data">
				<template slot-scope="{ row, index }" slot="provienceId">
				  <Row>
				    <Col>
				      <span @click="showpro(row.provienceId)">{{pro[index]}}</span>
				      
				    </Col>
				  </Row>
				</template>
				<template slot-scope="{ row, index }" slot="cityId">
				  <Row>
				    <Col>
				      <span @click="showcit(row.cityId)">{{cit[index]}}</span>
				      
				    </Col>
				  </Row>
				</template>
				<template slot-scope="{ row, index }" slot="reignId">
				  <Row>
				    <Col>
				      <span @click="showreg(row.reignId)">{{reg[index]}}</span>
				      
				    </Col>
				  </Row>
				</template>
				<template slot-scope="{ row, index }" slot="action">
					<Button type="primary" size="small" style="margin-right: 5px" @click="show(row.houseId)">详细</Button>
					<Button type="error" size="small" @click="remove(row.houseId)">删除</Button>
				</template>
			</Table>
			<Page :class="'m-2 text-right'" :page-size="tableInfo.pageSize" @on-change="pageSearch"
				:total="tableInfo.total" />
		</Layout>
	</Layout>
</template>
<script>
	export default {
		data() {
			let id = 0;
			return {
				props: {
					lazy: true,
					value: 'srId',
					label:'name',
					lazyLoad: (node, resolve) => { //node: 点击，lazyLoad : 加载数据
						console.log("node", node)
						const value = node.value || 0;
						const level = node.level;//当前点的第几集
						this.searchProvience(value).then(result=>{
							result.forEach( item => {
								item.leaf = level >= 2//leaf 判断是否是最后一集
							})
							console.log('result', result)
							resolve(result);//将result 放到
						})
					}
				},
				pro:['ww','sa','ad','fd','sa','fr','ad','vg','ge','wc'],
				cit:[],
				reg:[],
				searchDes: "",
				provinceId: "",
				cityId: "",
				reignId: "",
				sheng: [],
				shi: [],
				qu: [],
				provinces: [{
					id: "",
					label: ""
				}],
				citiys: [{
					id: "",
					label: ""
				}],
				regins: [{
					id: "",
					label: ""
				}],
				tableInfo: {
					currentPage: 1,
					total: 0,
					columnInfo: [{
							title: "房屋编号",
							key: "houseId",
							
						},
						{
							title: "省",
							slot: "provienceId",
							
						},
						{
							title: "市",
							slot: "cityId",
							
						},
						{
							title: "区",
							slot: "reignId",
							
						},

						{
							title: "面积",
							key: "area",
							
						},
						{
							title: "操作",
							slot: 'action',
							
						}
					],
					data: []
				}
			}
		},
		methods: {
			showpro:function(provienceId){
				this.http.get({
					url: "/sys-region/name",
					param: {
						srId:provienceId
					}
				}).then(data => {
					console.log(data)
					this.$data.pro.push(data)
					// console.log(this.pro)
				})
			},
			showcit:function(cityId){
				this.http.get({
					url: "/sys-region/name",
					param: {
						srId:cityId
					}
				}).then(data => {
					// console.log(data)
					this.$data.cit.push(data)
					// console.log(this.cit)
				})
			},
			showreg:function(reignId){
				this.http.get({
					url: "/sys-region/name",
					param: {
						srId:reignId
					}
				}).then(data => {
					// console.log(data)
					this.$data.reg.push(data)
					// console.log(this.cit)
				})
			},
			searchProvience: function(parentId) {
				return this.http.get({
					url: "/sys-region/list",
					param: {
						parentId
					}
				})
			},
			init: function() {
				this.search(1);
			},
			newHouse: function() {
				this.$router.push({
					name: "house_new"
				});
			},
			show: function(houseId) {
				this.$router.push({
					name: "house_detail",
					params: {
						houseId
					}
				});
			},
			remove: function(houseId) {
				this.message.confirm({
					title: "删除提示",
					content: "即将删除用户，确定吗？",
					onOk: () => {
						this.http.delete({
							url: "/house/info",
							param: {
								houseId
							}
						}).then(data => {
							this.search(this.$data.tableInfo.currentPage);
						});
					},
					onCancel: () => {
						this.message.info('操作已取消');
					}
				});
			},
			search: function(currentPage) {
				this.pro = []
				this.cit = []
				this.reg = []
				this.http.get({
					url: "/house/page",
					param: {
						currentPage: currentPage,
						des: this.$data.searchDes,
						provinceId: this.provinceId,
						cityId: this.cityId,
						reignId: this.reignId
					}
				}).then(data => {
					console.log(data.records)
					for(var i = 0;i<data.records.length;i++){
						console.log(i)
						this.showpro(data.records[i].provienceId)
						this.showcit(data.records[i].cityId)
						this.showreg(data.records[i].reignId)
						
					}
					// for(var j = 0;j<data.records.length;j++){
						
					// 	this.showcit(data.records[j].cityId)
					// }
					
					this.$data.tableInfo.data = data.records;
					this.$data.tableInfo.total = data.total;
					this.$data.tableInfo.pageSize = data.size;
				});
			},
			pageSearch: function(changedPage) {
				this.$data.tableInfo.currentPage = changedPage;
				this.search(changedPage);
			}
		},
		created: function() {
			this.init();
		}
	}
</script>
<style>
</style>
